import axios from "axios";
import { ElMessage } from "element-plus";

const request = axios.create({
  baseURL: "http://sph-api.atguigu.cn",
  timeout: 5000,
});
request.interceptors.request.use((config) => {
  const str = localStorage.getItem("user");
  config.headers.token = str ? JSON.parse(str).token : "";
  return config;
});

request.interceptors.response.use(
  (response) => {
    return response;
  },
  (err) => {
    const status = err.response;
    console.log("util/request.ts err.response", err);
    let msg = "";
    switch (status) {
      case 403:
        msg = "无权访问";
        break;
    }
    ElMessage({
      type: "error",
      message: msg,
    });
    return Promise.reject(err);
  },
);

export default request;
